#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>

#define flr(x,l,r) for (int x = l; x <= r; ++ x)
#define frl(x,r,l) for (int x = r; x >= l; -- x)
#define LL long long 
#define mt memset 
#define szf sizeof
#define INF 0x3f3f3f3f
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector 
#define pb push_back
#define sz size
#define PII pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 50010, M = 2e5 + 10;

int n, m;
int e[M], w[M], ne[M], h[N], idx;
int dist[6][N];
int source[6];
bool st[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void dijkstra(int start, int dist[]) {
    mt(dist, 0x3f, N * szf (int));
    dist[start] = 0;
    
    mt(st, 0, szf st);
    priority_queue<PII, vector<PII>, greater<PII> > heap;
    heap.push({0, start});
    
    while (heap.sz()) {
        PII t = heap.top();
        heap.pop();
        
        int ver = t.y;
        if (st[ver]) con;
        
        st[ver] = true;
        
        for (int i = h[ver]; ~i; i = ne[i]) {
            int j = e[i];
            if (dist[j] > dist[ver] + w[i]) {
                dist[j] = dist[ver] + w[i];
                heap.push({dist[j], j});
            }
        }
    }
}

int dfs(int u, int star, int distance) {
    if (u > 5) return distance;
    
    int res = INF;
    flr (i, 1, 5)
        if (!st[i]) {
            int next = source[i];
            st[i] = true;
            res = min(res, dfs(u + 1, i, distance + dist[star][next]));
            st[i] = false;
        }
    
    return res;
}

int main() {
	in(n), in(m);
	
	source[0] = 1;
	flr (i, 1, 5) in(source[i]);
	
	mt(h, -1, szf h);
	while (m -- ) {
	    int a, b, c;
	    in(a), in(b), in(c);
	    add(a, b, c), add(b, a, c);
	}
	
	flr (i, 0, 5) dijkstra(source[i], dist[i]);
	
	mt(st, 0, szf st);
	out(dfs(1, 0, 0)), pn;
	
	return 0;
}